package inits

import (
	"encoding/json"
	"fmt"
	"github.com/nacos-group/nacos-sdk-go/v2/clients"
	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
	"github.com/nacos-group/nacos-sdk-go/v2/vo"
	"log"
	"rpc-server/basic/appconfig"
	"rpc-server/basic/global"
)

func InitNacos() {

	clientConfig := constant.ClientConfig{
		NamespaceId:         appconfig.AppConf.NamespaceID, //we can create multiple clients with different namespaceId to support multiple namespace.When namespace is public, fill in the blank string here.
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "/tmp/nacos/log",
		CacheDir:            "/tmp/nacos/cache",
		LogLevel:            "debug",
	}
	serverConfigs := []constant.ServerConfig{
		{
			IpAddr:      appconfig.AppConf.Ipaddr,
			ContextPath: "/nacos",
			Port:        8848,
			Scheme:      "http",
		},
	}
	// Create config client for dynamic configuration
	configClient, _ := clients.CreateConfigClient(map[string]interface{}{
		"serverConfigs": serverConfigs,
		"clientConfig":  clientConfig,
	})

	content, _ := configClient.GetConfig(vo.ConfigParam{
		DataId: appconfig.AppConf.DataId,
		Group:  appconfig.AppConf.Group})

	fmt.Println(content)
	var my appconfig.Config
	json.Unmarshal([]byte(content), &my)
	global.Nacos = my
	log.Println(global.Nacos)

}
